package com.jfinal_demo.app.controllers;

import com.jfinal.core.Controller;
import com.jfinal_demo.app.authenticate.Authenticate;
import com.jfinal_demo.app.authenticate.LoginInfo;
import com.jfinal_demo.app.DataWrapper;

@Authenticate
public class BaseController extends Controller {
    private static final int DEFAULT_PAGENUMBER = 1;
    private static final int DEFAULT_PAGESIZE = 20;
    protected int pageNumber;
    protected int pageSize;

    protected void getPageInfo() {
        pageNumber = getParaToInt("pageNumber", DEFAULT_PAGENUMBER);
        pageSize = getParaToInt("pageSize", DEFAULT_PAGESIZE);
    }

    protected LoginInfo getLoginInfo() {
        return getSessionAttr(LoginInfo.SESSION_KEY);
    }

    @Override
    public void renderJson(Object object) {
        // wrap result
        if (!(object instanceof DataWrapper))
            object = DataWrapper.success(object);
        super.renderJson(object);
    }

    public void succeed() {
        super.renderJson(new DataWrapper());
    }
}
